<?php
namespace App\Commands;

use App\Models\Logic\UserLogic;
use Swoft\Console\Bean\Annotation\Command;
use Swoft\Console\Bean\Annotation\Mapping;
use Swoft\Console\Input\Input;
use Swoft\Console\Output\Output;
use Swoft\Core\Coroutine;
use Swoft\Log\Log;
use Swoft\Task\Task;
/**
 *init command
 *
 * @Command(name="myinit",coroutine=false)
 */
class InitCommand{

    /**
     * gen prikey and pubkey into redis
     *
     * @Usage
     * myinit:{command} [arguments] [options]
     *
     * @Options
     * -o,--o this is command option
     *
     * @Arguments
     * arg this is argument
     *
     * @Example
     * php swoft myinit:genkey arg=stelin -o opt
     *
     * @param Input  $input
     * @param Output $output
     *
     * @Mapping("genkey")
     */
    public function genKey(Input $input, Output $output)
    {
        $config = array(
            "digest_alg" => "sha512",
            "private_key_bits" => 4096,
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
        );
        $res = openssl_pkey_new($config);
        openssl_pkey_export($res, $privKey); //$privkey传入地址，因此此时可以得到私钥
        $pubKey = openssl_pkey_get_details($res);
        $pubKey = $pubKey["key"]; //这里是公钥
        cache()->set("prikey",$privKey);
        cache()->set("pubkey",$pubKey);
        $output->writeln("init key done~~~");
    }
}
